import base64
import hashlib
import io
import os
import random
# import paddlehub as hub
import re
import subprocess as sub
import threading
import time
import zipfile
from string import whitespace
from tkinter import *
from tkinter import filedialog
from tkinter.scrolledtext import ScrolledText
from urllib.parse import quote, unquote

import cv2
import emoji
import pandas as pd
import py7zr
import qrcode
import rarfile
from PIL import Image, ImageTk
from pymouse import PyMouse

'''
自娱工具箱_v1.0 
主要包含功能：文件合并、文件删除、md5编码、url编码解码、视频下载、鼠标重复点击事件辅助功能
'''


class MY_GUI():
    def __init__(self, init_window_name):
        self.init_window_name = init_window_name
        self.bg_file_path = None

    # 设置背景长宽类型 1 全部 2 横屏 3 竖屏

    # 每次切换功能前销毁所有grid组件
    def init_text(self):
        for i in self.init_window_name.grid_slaves():
            i.destroy()
        self.img = self.get_bg_img()
        self.pil_image = Image.open(self.img).resize((1000, 600))
        self.bg_file_path_e = None
        self.bg_set()

    def bg_set(self):
        file_paths = []
        user_path = os.path.expanduser('~')
        bg_set_dir = os.path.join(user_path, "bg_set_dir.myguitest")
        if os.path.exists(bg_set_dir):
            ttt = open(bg_set_dir, encoding='utf-8')
            t = ttt.read().split("$$")
            self.bg_show_type = int(t[0])
            self.bg_file_path = t[1]
        if self.bg_file_path_e is not None and not "".__eq__(self.bg_file_path_e.get()):
            self.bg_file_path = self.bg_file_path_e.get()
            self.bg_show_type = self.r.get()
            with open(bg_set_dir, 'w', encoding='utf-8') as f:
                f.write(str(self.bg_show_type) + "$$")
                f.write(self.bg_file_path)
        if self.bg_file_path is not None:
            filenames = os.listdir(self.bg_file_path)
            for filename in filenames:
                if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".gif"):
                    filename = os.path.join(self.bg_file_path, filename)
                    file_paths.append(filename)
            intt = len(file_paths)
            if intt > 0:
                while True:
                    self.pil_image = Image.open(file_paths[random.randint(0, intt - 1)])
                    (x, y) = self.pil_image.size
                    if x > y and (1 == self.bg_show_type or 0 == self.bg_show_type):
                        self.pil_image = self.pil_image.resize((1000, int(y * 1000 / x)))
                        break
                    if x <= y and (2 == self.bg_show_type or 0 == self.bg_show_type):
                        self.pil_image = self.pil_image.resize((int(x * 600 / y), 600))
                        break
            # self.pil_image = Image.open(file_paths[random.randint(0, intt - 1)]).resize((1000, 600))
        self.tk_image = ImageTk.PhotoImage(self.pil_image)
        self.im_label_root = Label(self.init_window_name, image=self.tk_image, width=1000, height=600, compound=CENTER)
        self.im_label_root.grid(row=0, column=0, rowspan=20, columnspan=20)

    def bg_set_dirpath(self):
        self.init_text()
        self.init_bg_label = Label(self.init_window_name, text="壁纸文件夹路径:", width=40)
        self.init_bg_label.grid(sticky=E, row=1, column=0)
        self.init_bg_label = Label(self.init_window_name, text="壁纸展示类型:", width=40)
        self.init_bg_label.grid(sticky=E, row=2, column=0)
        # 文本框
        self.bg_file_path_e = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.bg_file_path_e.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_folder(self.bg_file_path_e))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.r = IntVar()
        self.radio1 = Radiobutton(self.init_window_name, text="全部", value=0, variable=self.r)
        self.radio1.grid(sticky=W, row=2, column=1)
        self.radio2 = Radiobutton(self.init_window_name, text="横屏", value=1, variable=self.r)
        self.radio2.grid(sticky=W, row=2, column=2)
        self.radio3 = Radiobutton(self.init_window_name, text="竖屏", value=2, variable=self.r)
        self.radio3.grid(sticky=W, row=2, column=3)
        self.button = Button(self.init_window_name, text="确认", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.bg_set()))
        self.button.grid(sticky=W, row=1, column=13)

    # 设置窗口
    def set_init_window(self):
        self.init_window_name.title("自娱工具箱_v1.0")  # 窗口名
        self.init_window_name.geometry('1000x600+400+10')  # 1000 600为窗口大小，+10 +10 定义窗口弹出时的默认展示位置
        self.init_window_name.resizable(False, False)
        self.init_window_name.option_add('*tearOff', False)  # 去除多级目录中横虚线
        # self.init_window_name["bg"] = "LightCyan" # 窗口背景色，其他背景色见：blog.csdn.net/chl0000/article/details/7657887
        self.init_window_name.attributes("-alpha", 0.9)  # 虚化，值越小虚化程度越高
        self.init_text()

        # 菜单
        # 一级目录
        menu11 = Menu(self.init_window_name)
        # 二级目录
        menu21 = Menu(menu11)  # 数据操作
        menu21.add_command(label="url编码", command=self.urlquote_windows)
        menu21.add_command(label="url解码", command=self.urlunquote_windows)
        menu21.add_command(label="md5加密", command=self.str_trans_to_md5_windows)
        menu21.add_command(label="生成二维码", command=self.qrcode_window)
        menu31 = Menu(menu11)  # 文件操作
        menu31.add_command(label="文本合并", command=self.concat_windows)
        menu31.add_command(label="excel合并相同下标sheet", command=self.concatexcel_0_windows)
        menu31.add_command(label="excel合并成一个sheet", command=self.concatexcel_1_windows)
        menu31.add_command(label="删除重复文件", command=self.deletduplicatefile_windows)
        menu31.add_command(label="删除名称包含指定字段的文件", command=self.deletFileContainsStr_windows)
        menu31.add_command(label="删除指定大小的文件", command=self.deletFileWithSize_windows)
        menu31.add_command(label="复制指定名称文件到指定路径", command=self.copyFileContainsStr_windows)
        menu31.add_command(label="移动指定名称文件到指定路径", command=self.cutFileContainsStr_windows)
        menu31.add_command(label="文件转base64", command=self.read_file_to_base64_window)
        menu31.add_command(label="base64转文件", command=self.base64_to_file_window)
        menu31.add_command(label="批量解压文件", command=self.unzip_windows)
        menu41 = Menu(menu11)  # 图片视频操作
        menu41.add_command(label="视频提取图片", command=self.video_to_img_window)
        menu41.add_command(label="视频调整", command=self.resize_video_window)
        # menu41.add_command(label="图片去除背景", command=self.deleteBg_windows)
        menu51 = Menu(menu11)  # 输入设备控制
        menu51.add_command(label="鼠标左键重复点击辅助", command=self.mouse_left_click_windows)

        menu11.add_command(label="首页", command=self.init_text)
        menu11.add_command(label="壁纸设置", command=self.bg_set_dirpath)
        menu11.add_command(label="下载视频", command=self.download_video_window)
        menu11.add_cascade(label="数据操作", menu=menu21)
        menu11.add_cascade(label="文件操作", menu=menu31)
        menu11.add_cascade(label="图片视频操作", menu=menu41)
        menu11.add_cascade(label="输入设备控制", menu=menu51)
        self.init_window_name.config(menu=menu11)

    def unzip_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要解压文件文件夹路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label = Label(self.init_window_name, text="请输入解压密码（可不输入）:", width=40)
        self.init_data_label.grid(sticky=E, row=2, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=4, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_folder(self.file_path))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.password = Text(self.init_window_name, width=75, height=2)  # 输入密码
        self.password.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=4, column=1, rowspan=5, columnspan=10)
        self.button = Button(self.init_window_name, text="解压", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.unzip))
        self.button.grid(sticky=W, row=1, column=13)

    # 功能函数
    def unzip(self):
        try:
            info = ""
            password = self.password.get(1.0, END).strip().replace("\n", "").encode()
            for dirpath, dirnames, filenames in os.walk(self.file_path.get()):
                for filename in filenames:
                    try:
                        inputfilename = os.path.join(dirpath, filename)
                        if filename.lower().endswith('.rar'):
                            with rarfile.RarFile(inputfilename, 'r') as rar_ref:
                                if password:
                                    rar_ref.extractall(dirpath, pwd=password)
                                else:
                                    rar_ref.extractall(dirpath)
                            info += "unzip success:" + inputfilename + "\n"
                        elif filename.lower().endswith('.zip'):
                            with zipfile.ZipFile(inputfilename, 'r') as zip_ref:
                                if password:
                                    zip_ref.extractall(inputfilename, pwd=password)
                                else:
                                    zip_ref.extractall(inputfilename)
                            info += "unzip success:" + inputfilename + "\n"
                        elif filename.lower().endswith('.7z'):
                            if password:
                                with py7zr.SevenZipFile(inputfilename, mode='r', password=str(password)) as z:
                                    z.extractall(dirpath)
                            else:
                                with py7zr.SevenZipFile(inputfilename, mode='r') as z:
                                    z.extractall(dirpath)
                            info += "un7z success:" + inputfilename + "\n"

                    except FileExistsError as f:
                        print(f)
                        info = f
                        self.result_data_Text.delete(1.0, END)
                        self.result_data_Text.insert(1.0, info + "\n")
                        continue
        except RuntimeError as af:
            info = af
        self.result_data_Text.delete(1.0, END)
        self.result_data_Text.insert(1.0, info)

    def str_trans_to_md5_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="请输入需要加密的数据:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出加密结果:", width=40)
        self.result_data_label.grid(sticky=E, row=2, column=0)
        # 文本框
        self.file_path = Text(self.init_window_name, width=75, height=2)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=2, column=1, rowspan=5, columnspan=10)
        self.button = Button(self.init_window_name, text="确认", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.str_trans_to_md5))
        self.button.grid(sticky=W, row=1, column=12)

    # 功能函数
    def str_trans_to_md5(self):
        src = self.file_path.get(1.0, END).strip().replace("\n", "").encode()
        if src:
            try:
                myMd5 = hashlib.md5()
                myMd5.update(src)
                myMd5_Digest = myMd5.hexdigest()
                # 输出到界面
                self.result_data_Text.delete(1.0, END)
                self.result_data_Text.insert(1.0, myMd5_Digest)
            except RuntimeError as af:
                self.result_data_Text.delete(1.0, END)
                self.result_data_Text.insert(1.0, "字符串转MD5失败")
                self.result_data_Text.insert(1.0, af)
        else:
            self.write_log_to_Text("ERROR:str_trans_to_md5 failed")

    # 将sql或者文本合并起来
    def concat_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要合并文件文件夹路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="指定文件名（合并数据到该文件）:", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.init_data_label3 = Label(self.init_window_name, text="需要合并的文件的格式，如 .sql:", width=40)
        self.init_data_label3.grid(sticky=E, row=3, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=4, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_folder(self.file_path))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.except_filename = Text(self.init_window_name, width=75, height=2)  # 合并后文件名或者包含指定字段需要删除的文件名
        self.except_filename.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10)
        self.lastcode = Text(self.init_window_name, width=75, height=2)  # 需要合并的文件的格式
        self.lastcode.grid(sticky=W, row=3, column=1, rowspan=1, columnspan=10)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=4, column=1, rowspan=5, columnspan=10)
        self.button = Button(self.init_window_name, text="合并", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.concat))
        self.button.grid(sticky=W, row=1, column=13)

    def concat(self):
        try:
            self.delete_except_file()
            info = ""
            for dirpath, dirnames, filenames in os.walk(
                    self.file_path.get()):
                for filename in filenames:
                    inputfilename = ""
                    try:
                        if filename == self.except_filename.get(1.0, END).replace("\n",
                                                                                  "") or not filename.__contains__(
                            self.lastcode.get(1.0, END).replace("\n", "")):
                            continue
                        inputfilename = dirpath.replace("\\", "/") + '/' + filename
                        with open(
                                dirpath.replace("\\", "/") + '/' + self.except_filename.get(1.0, END).replace("\n", ""),
                                "a+", encoding="utf-8") as ff:
                            ff.write("\n" + "-- ----------------------------" + "\n")
                            ff.write("-- " + filename + "\n")
                            ff.write("-- ----------------------------" + "\n")
                            ff.write(open(inputfilename, "r", encoding="utf-8").read())
                            print(
                                "concatfile success:" + inputfilename + " to " + self.except_filename.get(1.0,
                                                                                                          END).replace(
                                    "\n", ""))
                            info += "concatfile success:" + inputfilename + " to " + self.except_filename.get(1.0,
                                                                                                              END).replace(
                                "\n", "") + "\n"
                    except FileExistsError as f:
                        print(f)
                        info = f
                        self.result_data_Text.delete(1.0, END)
                        self.result_data_Text.insert(1.0,
                                                     "concatfile error:" + inputfilename + " to " + self.except_filename.get(
                                                         1.0, END).replace("\n", "") + "\n")
                        continue
        except RuntimeError as af:
            info = af
        self.result_data_Text.delete(1.0, END)
        self.result_data_Text.insert(1.0, info)

    def concatexcel_0_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要合并文件文件夹路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="指定文件名（合并数据到该文件）:", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.init_data_label3 = Label(self.init_window_name, text="需要合并的文件的格式，如 .sql:", width=40)
        self.init_data_label3.grid(sticky=E, row=3, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=4, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_folder(self.file_path))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.except_filename = Text(self.init_window_name, width=75, height=2)  # 合并后文件名或者包含指定字段需要删除的文件名
        self.except_filename.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10)
        self.lastcode = Text(self.init_window_name, width=75, height=2)  # 需要合并的文件的格式
        self.lastcode.grid(sticky=W, row=3, column=1, rowspan=1, columnspan=10)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=4, column=1, rowspan=5, columnspan=10)
        self.button = Button(self.init_window_name, text="合并", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.concatexcel_0))
        self.button.grid(sticky=W, row=1, column=13)

    # 将excel合并起来(type 0 合并excel sheet下标相同才合并)
    def concatexcel_0(self):
        self.delete_except_file()
        # 存放index相同的sheet的数据
        frames = {}
        for i in range(100):
            frames[i] = []
        max_sheet_index = 1
        info = ""
        try:
            for dirpath, dirnames, filenames in os.walk(
                    self.file_path.get()):
                for filename in filenames:
                    inputfilename = ""
                    try:
                        if filename == self.except_filename.get(1.0, END).replace("\n",
                                                                                  "") or not filename.__contains__(
                            self.lastcode.get(1.0, END).replace("\n", "")):
                            continue
                        inputfilename = dirpath.replace("\\", "/") + '/' + filename
                        file = pd.ExcelFile(inputfilename)
                        print(file.sheet_names)
                        max_sheet_index_temp = file.sheet_names.__len__()
                        if max_sheet_index_temp > max_sheet_index:
                            max_sheet_index = max_sheet_index_temp
                        for i in range(max_sheet_index_temp):
                            print(file.sheet_names[i])
                            df = pd.read_excel(inputfilename, sheet_name=i)  # excel转换成DataFrame
                            frames[i].append(df)
                        print(
                            "concatfile success:" + inputfilename + " to " + self.except_filename.get(1.0, END).replace(
                                "\n", ""))
                        info += "concatfile success:" + inputfilename + " to " + self.except_filename.get(1.0,
                                                                                                          END).replace(
                            "\n", "") + "\n"
                    except FileExistsError as f:
                        print(f)
                        info = f
                        self.result_data_Text.delete(1.0, END)
                        self.result_data_Text.insert(1.0,
                                                     "concatfile error:" + inputfilename + " to " + self.except_filename.get(
                                                         1.0, END).replace("\n", "") + "\n")
                        continue
            writer = pd.ExcelWriter(
                self.file_path.get() + "/" + self.except_filename.get(1.0, END).replace(
                    "\n", ""))
            for i in range(max_sheet_index):  # 写入不同的sheet
                result = pd.concat(frames[i])
                result.to_excel(writer, sheet_name="sheet" + str(i), index=0)  # 设置第1列为索引列
            writer.save()
        except RuntimeError as af:
            print(af)
            info = af
        self.result_data_Text.delete(1.0, END)
        self.result_data_Text.insert(1.0, info)

    def concatexcel_1_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要合并文件文件夹路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="指定文件名（合并数据到该文件）:", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.init_data_label3 = Label(self.init_window_name, text="需要合并的文件的格式，如 .sql:", width=40)
        self.init_data_label3.grid(sticky=E, row=3, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=4, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_folder(self.file_path))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.except_filename = Text(self.init_window_name, width=75, height=2)  # 合并后文件名或者包含指定字段需要删除的文件名
        self.except_filename.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10)
        self.lastcode = Text(self.init_window_name, width=75, height=2)  # 需要合并的文件的格式
        self.lastcode.grid(sticky=W, row=3, column=1, rowspan=1, columnspan=10)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=4, column=1, rowspan=5, columnspan=10)
        self.button = Button(self.init_window_name, text="合并", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.concatexcel_1))
        self.button.grid(sticky=W, row=1, column=13)

    # 将excel合并起来(1 合并excel所有sheet合为一个sheet)
    def concatexcel_1(self):
        self.delete_except_file()
        # 存放index相同的sheet的数据
        frames = {}
        for i in range(100):
            frames[i] = []
        max_sheet_index = 1
        info = ""
        try:
            for dirpath, dirnames, filenames in os.walk(
                    self.file_path.get()):
                for filename in filenames:
                    inputfilename = ""
                    try:
                        if filename == self.except_filename.get(1.0, END).replace("\n",
                                                                                  "") or not filename.__contains__(
                            self.lastcode.get(1.0, END).replace("\n", "")):
                            continue
                        inputfilename = dirpath.replace("\\", "/") + '/' + filename
                        file = pd.ExcelFile(inputfilename)
                        print(file.sheet_names)
                        max_sheet_index_temp = file.sheet_names.__len__()
                        if max_sheet_index_temp > max_sheet_index:
                            max_sheet_index = max_sheet_index_temp
                        for i in range(max_sheet_index_temp):
                            print(file.sheet_names[i])
                            df = pd.read_excel(inputfilename, sheet_name=i)  # excel转换成DataFrame
                            frames[i].append(df)
                        print(
                            "concatfile success:" + inputfilename + " to " + self.except_filename.get(1.0, END).replace(
                                "\n", ""))
                        info += "concatfile success:" + inputfilename + " to " + self.except_filename.get(1.0,
                                                                                                          END).replace(
                            "\n", "") + "\n"
                    except FileExistsError as f:
                        print(f)
                        self.result_data_Text.delete(1.0, END)
                        self.result_data_Text.insert(1.0,
                                                     "concatfile error:" + inputfilename + " to " + self.except_filename.get(
                                                         1.0, END).replace("\n", "") + "\n")
                        continue
            writer = pd.ExcelWriter(
                self.file_path.get() + "/" + self.except_filename.get(1.0, END).replace(
                    "\n", ""))

            frames_2 = []
            for i in range(max_sheet_index):  # 写入不同的sheet
                frames_2.extend(frames[i])
            result = pd.concat(frames_2)
            result.to_excel(writer, sheet_name="sheet1", index=0)  # 设置第1列为索引列
            writer.save()
        except RuntimeError as af:
            print(af)
        self.result_data_Text.delete(1.0, END)
        self.result_data_Text.insert(1.0, info)

    # 获取当前时间
    def get_current_time(self):
        current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        return current_time

    # 日志动态打印
    def write_log_to_Text(self, logmsg):
        global LOG_LINE_NUM
        current_time = self.get_current_time()
        logmsg_in = str(current_time) + " " + str(logmsg) + "\n"  # 换行
        if LOG_LINE_NUM <= 7:
            self.log_data_Text.insert(END, logmsg_in)
            LOG_LINE_NUM = LOG_LINE_NUM + 1
        else:
            self.log_data_Text.delete(1.0, 2.0)
            self.log_data_Text.insert(END, logmsg_in)

    # 删除指定文件
    def delete_except_file(self):
        try:
            if os.path.exists(
                    self.file_path.get() + "/" + self.except_filename.get(1.0, END).replace(
                        "\n", "")):
                pathss = self.file_path.get() + "/" + self.except_filename.get(1.0,
                                                                               END).replace(
                    "\n", "")
                print(pathss)
                os.remove(pathss)
        except FileExistsError as f:
            print(f)
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, f + "\n")

    def urlquote_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要编码的url:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.result_data_label = Label(self.init_window_name, text="处理结果:", width=40)
        self.result_data_label.grid(sticky=E, row=2, column=0)
        # 文本框
        self.file_path = Text(self.init_window_name, width=75, height=2)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=2, column=1, rowspan=7, columnspan=10)
        self.button = Button(self.init_window_name, text="编码", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.urlquote))
        self.button.grid(sticky=W, row=1, column=12)

    def urlquote(self):
        try:
            text = quote(self.file_path.get(1.0, END).replace("\n", ""), "utf-8")
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, text)
        except:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, "编码失败")

    def urlunquote_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要解码的url:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.result_data_label = Label(self.init_window_name, text="处理结果:", width=40)
        self.result_data_label.grid(sticky=E, row=2, column=0)
        # 文本框
        self.file_path = Text(self.init_window_name, width=75, height=2)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=2, column=1, rowspan=7, columnspan=10)
        self.button = Button(self.init_window_name, text="解码", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.urlunquote))
        self.button.grid(sticky=W, row=1, column=12)

    def urlunquote(self):
        try:
            text = unquote(self.file_path.get(1.0, END).replace("\n", ""), "utf-8")
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, text)
        except:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, "解码失败")

    def calcsha1(self, path1):
        with open(path1, "rb") as ff:
            sha1obj = hashlib.sha1()
            sha1obj.update(ff.read())
            hash = sha1obj.hexdigest()
            return hash

    def calcMD5(self, path1):
        with open(path1, "rb") as ff:
            md5obj = hashlib.md5()
            md5obj.update(ff.read())
            hash = md5obj.hexdigest()
            return hash

    def deletduplicatefile_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要处理的文件夹路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.result_data_label = Label(self.init_window_name, text="处理结果:", width=40)
        self.result_data_label.grid(sticky=E, row=2, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_folder(self.file_path))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=2, column=1, rowspan=7, columnspan=10)
        self.button = Button(self.init_window_name, text="删除", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.deletduplicatefile))
        self.button.grid(sticky=W, row=1, column=13)

    # 根据hash值删除重复文件
    def deletduplicatefile(self):
        try:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, "正在处理请等待.......\n")
            starttime = time.time()
            hashs = []
            filesizes = []
            filepaths = []
            count = 0
            delcount = 0
            for dirpath, dirnames, filenames in os.walk(self.file_path.get()):
                for filename in filenames:
                    count += 1
                    filepath = os.path.join(dirpath, filename)
                    print(filepath)
                    if not filesizes.__contains__(os.path.getsize(filepath)):
                        filepaths.append(filepath)
            for filepath in filepaths:
                hash = self.calcMD5(filepath)
                if hashs.__contains__(hash):
                    os.remove(filepath)
                    delcount += 1
                else:
                    hashs.append(hash)
            usedtime = time.time() - starttime
            print(usedtime)
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(END, "删除重复文件个数：" + str(delcount) + "\n")
            self.result_data_Text.insert(END, "扫描到文件个数：" + str(count) + "\n")
            self.result_data_Text.insert(END, "扫描总时间：" + str(usedtime) + "s\n")
        except RuntimeError as rte:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, rte)

    # 检查字符串包含关系str1是否包含str2中的字符串
    def check_str_contains(self, str1, str2):
        str22 = []
        if "".__eq__(str2):
            return False
        if str2.__contains__(";"):
            str22 = str2.split(";")
        elif str2.__contains__("；"):
            str22 = str2.split("；")
        else:
            str22.append(str2)
        for s2 in str22:
            if str1.__contains__(s2):
                return True
        return False

    def copyFileContainsStr_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要处理的文件夹路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="指定文件名包含字段(多个字段使用;隔开):", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.init_data_label4 = Label(self.init_window_name, text="指定文件名排除字段(多个字段使用;隔开):", width=40)
        self.init_data_label4.grid(sticky=E, row=3, column=0)
        self.init_data_label3 = Label(self.init_window_name, text="复制到文件夹路径:", width=40)
        self.init_data_label3.grid(sticky=E, row=4, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=5, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_folder(self.file_path))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.include_filename = Entry(self.init_window_name, width=75)  # 包含指定字段需要复制的文件名
        self.include_filename.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10, ipady=5)
        self.except_filename = Entry(self.init_window_name, width=75)  # 包含指定字段需要不复制的文件名
        self.except_filename.grid(sticky=W, row=3, column=1, rowspan=1, columnspan=10, ipady=5)
        self.file_path2 = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path2.grid(sticky=W, row=4, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies2 = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                             command=lambda: self.browse_folder(self.file_path2))
        self.browse_folder_cookies2.grid(sticky=W, row=4, column=12)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=5, column=1, rowspan=6, columnspan=10)
        self.button = Button(self.init_window_name, text="开始复制", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.copyFileContainsStr))
        self.button.grid(sticky=W, row=1, column=13)

    # 复制包含指定字段的文件名的文件
    def copyFileContainsStr(self):
        try:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, "正在处理请等待.......\n")
            starttime = time.time()
            count = 0
            delcount = 0
            for dirpath, dirnames, filenames in os.walk(self.file_path.get()):
                for filename in filenames:
                    count += 1
                    filepath = os.path.join(dirpath, filename)
                    if self.check_str_contains(filepath, self.include_filename.get().replace("\n",
                                                                                             "")) and not self.check_str_contains(
                        filepath, self.except_filename.get().replace("\n", "")) and not os.path.normcase(
                        dirpath) == os.path.normcase(self.file_path2.get()):
                        namepre = ''
                        if os.path.exists(os.path.join(self.file_path2.get(), filename)):
                            namepre = str(time.time())
                        newfilepath = os.path.join(self.file_path2.get(), namepre + filename)
                        print(filepath)
                        with open(newfilepath, "wb") as f:
                            f.write(open(filepath, "rb").read())
                            self.result_data_Text.insert(END, "文件复制：" + filepath + " to " + newfilepath + "\n")
                            self.result_data_Text.yview_moveto(1)
                        delcount += 1
            usedtime = time.time() - starttime
            print(usedtime)
            self.result_data_Text.insert(END, "使用总时间：" + str(usedtime) + "s\n")
        except RuntimeError as rte:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, rte)

    def cutFileContainsStr_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要处理的文件夹路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="指定文件名包含字段(多个字段使用;隔开):", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.init_data_label4 = Label(self.init_window_name, text="指定文件名排除字段(多个字段使用;隔开):", width=40)
        self.init_data_label4.grid(sticky=E, row=3, column=0)
        self.init_data_label3 = Label(self.init_window_name, text="移动文件到文件夹路径:", width=40)
        self.init_data_label3.grid(sticky=E, row=4, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=5, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_folder(self.file_path))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.include_filename = Entry(self.init_window_name, width=75)  # 包含指定字段需要删除的文件名
        self.include_filename.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10, ipady=5)
        self.except_filename = Entry(self.init_window_name, width=75)  # 包含指定字段需要删除的文件名
        self.except_filename.grid(sticky=W, row=3, column=1, rowspan=1, columnspan=10, ipady=5)
        self.file_path2 = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path2.grid(sticky=W, row=4, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies2 = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                             command=lambda: self.browse_folder(self.file_path2))
        self.browse_folder_cookies2.grid(sticky=W, row=4, column=12)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=5, column=1, rowspan=6, columnspan=10)
        self.button = Button(self.init_window_name, text="开始移动", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.cutFileContainsStr))
        self.button.grid(sticky=W, row=1, column=13)

    # 移动包含指定字段的文件名的文件
    def cutFileContainsStr(self):
        try:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(END, "正在处理请等待.......\n")
            self.result_data_Text.yview_moveto(1)
            starttime = time.time()
            count = 0
            delcount = 0
            for dirpath, dirnames, filenames in os.walk(self.file_path.get()):
                for filename in filenames:
                    count += 1
                    filepath = os.path.join(dirpath, filename)
                    if self.check_str_contains(filepath, self.include_filename.get().replace("\n",
                                                                                             "")) and not self.check_str_contains(
                        filepath, self.except_filename.get().replace("\n", "")) and not os.path.normcase(
                        dirpath) == os.path.normcase(self.file_path2.get()):
                        namepre = ''
                        if os.path.exists(os.path.join(self.file_path2.get(), filename)):
                            namepre = str(time.time())
                        newfilepath = os.path.join(self.file_path2.get(), namepre + filename)
                        print(filepath)
                        with open(newfilepath, "wb") as f:
                            f.write(open(filepath, "rb").read())
                            os.remove(filepath)
                            self.result_data_Text.insert(END, "文件移动：" + filepath + " to " + newfilepath + "\n")
                            self.result_data_Text.yview_moveto(1)
                        delcount += 1
                if len(os.listdir(dirpath)) == 0:
                    os.rmdir(dirpath)
            usedtime = time.time() - starttime
            print(usedtime)
            self.result_data_Text.insert(END, "使用总时间：" + str(usedtime) + "s\n")
        except RuntimeError as rte:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, rte)

    def deletFileContainsStr_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要处理的文件夹路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="指定文件名包含字段(多个字段使用;隔开):", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=3, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_folder(self.file_path))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.except_filename = Entry(self.init_window_name, width=75)  # 包含指定字段需要删除的文件名
        self.except_filename.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10, ipady=5)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=3, column=1, rowspan=6, columnspan=10)
        self.button = Button(self.init_window_name, text="开始删除", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.deletFileContainsStr))
        self.button.grid(sticky=W, row=1, column=13)

    # 删除包含指定字段的文件名的文件
    def deletFileContainsStr(self):
        try:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, "正在处理请等待.......\n")
            self.result_data_Text.yview_moveto(1)
            starttime = time.time()
            count = 0
            delcount = 0
            for dirpath, dirnames, filenames in os.walk(self.file_path.get()):
                if 0 == len(os.listdir(dirpath)):
                    os.rmdir(dirpath)
                    self.result_data_Text.insert(END, "删除文件夹：" + dirpath + "\n")
                    self.result_data_Text.yview_moveto(1)
                for filename in filenames:
                    count += 1
                    if self.check_str_contains(filename, self.except_filename.get().replace("\n", "")):
                        filepath = os.path.join(dirpath, filename)
                        print(filepath)
                        os.remove(filepath)
                        self.result_data_Text.insert(END, "删除文件：" + filepath + "\n")
                        self.result_data_Text.yview_moveto(1)
                        delcount += 1
                    if 0 == len(os.listdir(dirpath)):
                        os.rmdir(dirpath)
                        self.result_data_Text.insert(END, "删除文件夹：" + dirpath + "\n")
                        self.result_data_Text.yview_moveto(1)
            usedtime = time.time() - starttime
            print(usedtime)
            self.result_data_Text.insert(END, "扫描总时间：" + str(usedtime) + "s\n")
            self.result_data_Text.yview_moveto(1)
            self.result_data_Text.insert(END, "删除文件个数：" + str(delcount) + "\n")
            self.result_data_Text.yview_moveto(1)
            self.result_data_Text.insert(END, "扫描到文件个数：" + str(count) + "\n")
            self.result_data_Text.yview_moveto(1)
        except RuntimeError as rte:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, rte)

    def deletFileWithSize_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要处理的文件夹路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="输入文件尺寸:", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.init_data_label3 = Label(self.init_window_name, text="尺寸单位:", width=40)
        self.init_data_label3.grid(sticky=E, row=3, column=0)
        self.init_data_label3 = Label(self.init_window_name, text="指定尺寸匹配规则:", width=40)
        self.init_data_label3.grid(sticky=E, row=4, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=5, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_folder(self.file_path))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.file_size = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_size.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10, ipady=5)
        self.rr = IntVar()  # 单位
        self.radio11 = Radiobutton(self.init_window_name, text="KB", value=0, variable=self.rr)
        self.radio11.grid(sticky=W, row=3, column=1)
        self.radio22 = Radiobutton(self.init_window_name, text="MB", value=1, variable=self.rr)
        self.radio22.grid(sticky=W, row=3, column=2)
        self.radio33 = Radiobutton(self.init_window_name, text="GB", value=2, variable=self.rr)
        self.radio33.grid(sticky=W, row=3, column=3)
        self.r = IntVar()  # 匹配规则
        self.radio1 = Radiobutton(self.init_window_name, text="大于", value=0, variable=self.r)
        self.radio1.grid(sticky=W, row=4, column=1)
        self.radio2 = Radiobutton(self.init_window_name, text="等于", value=1, variable=self.r)
        self.radio2.grid(sticky=W, row=4, column=2)
        self.radio3 = Radiobutton(self.init_window_name, text="小于", value=2, variable=self.r)
        self.radio3.grid(sticky=W, row=4, column=3)
        self.radio4 = Radiobutton(self.init_window_name, text="大于等于", value=3, variable=self.r)
        self.radio4.grid(sticky=W, row=4, column=4)
        self.radio5 = Radiobutton(self.init_window_name, text="小于等于", value=4, variable=self.r)
        self.radio5.grid(sticky=W, row=4, column=5)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=5, column=1, rowspan=6, columnspan=10)
        self.button = Button(self.init_window_name, text="开始删除", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.deletFileWithSize))
        self.button.grid(sticky=W, row=1, column=13)

    # 删除指定大小的文件
    def deletFileWithSize(self):
        try:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, "正在处理请等待.......\n")
            self.result_data_Text.yview_moveto(1)
            starttime = time.time()
            count = 0
            delcount = 0
            target_size = 0
            KB, MB, GB = 1024, 1024 ** 2, 1024 ** 3
            if 0 == self.rr.get():
                target_size = int(self.file_size.get()) * KB
            if 1 == self.rr.get():
                target_size = int(self.file_size.get()) * MB
            if 2 == self.rr.get():
                target_size = int(self.file_size.get()) * GB
            for dirpath, dirnames, filenames in os.walk(self.file_path.get()):
                for filename in filenames:
                    count += 1
                    filepath = os.path.join(dirpath, filename)
                    print(filepath)
                    print(os.path.getsize(filepath))
                    print(int(target_size))
                    if 0 == self.r.get() and os.path.getsize(filepath) > target_size:
                        os.remove(filepath)
                        self.result_data_Text.insert(END, "删除文件：" + filepath + "\n")
                        self.result_data_Text.yview_moveto(1)
                        delcount += 1
                    if 1 == self.r.get() and os.path.getsize(filepath) == target_size:
                        os.remove(filepath)
                        self.result_data_Text.insert(END, "删除文件：" + filepath + "\n")
                        self.result_data_Text.yview_moveto(1)
                        delcount += 1
                    if 2 == self.r.get() and os.path.getsize(filepath) < target_size:
                        os.remove(filepath)
                        self.result_data_Text.insert(END, "删除文件：" + filepath + "\n")
                        self.result_data_Text.yview_moveto(1)
                        delcount += 1
                    if 3 == self.r.get() and os.path.getsize(filepath) >= target_size:
                        os.remove(filepath)
                        self.result_data_Text.insert(END, "删除文件：" + filepath + "\n")
                        self.result_data_Text.yview_moveto(1)
                        delcount += 1
                    if 4 == self.r.get() and os.path.getsize(filepath) <= target_size:
                        os.remove(filepath)
                        self.result_data_Text.insert(END, "删除文件：" + filepath + "\n")
                        self.result_data_Text.yview_moveto(1)
                        delcount += 1
            usedtime = time.time() - starttime
            print(usedtime)
            self.result_data_Text.insert(END, "扫描总时间：" + str(usedtime) + "s\n")
            self.result_data_Text.yview_moveto(1)
            self.result_data_Text.insert(END, "删除文件个数：" + str(delcount) + "\n")
            self.result_data_Text.yview_moveto(1)
            self.result_data_Text.insert(END, "扫描到文件个数：" + str(count) + "\n")
            self.result_data_Text.yview_moveto(1)
        except RuntimeError as rte:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, rte)

    # 重复点击鼠标辅助
    def mouse_left_click_windows(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="鼠标左键点击次数:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="延迟几秒后开始任务:", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.init_data_label3 = Label(self.init_window_name, text="点击鼠标左键间隔时间", width=40)
        self.init_data_label3.grid(sticky=E, row=3, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=4, column=0)
        # 文本框
        self.click_count = Text(self.init_window_name, width=75, height=2)  # 输入的文件夹路径为
        self.click_count.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10)
        self.start_time = Text(self.init_window_name, width=75, height=2)  # 合并后文件名或者包含指定字段需要删除的文件名
        self.start_time.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10)
        self.run_after_time = Text(self.init_window_name, width=75, height=2)  # 需要合并的文件的格式
        self.run_after_time.grid(sticky=W, row=3, column=1, rowspan=1, columnspan=10)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=4, column=1, rowspan=5, columnspan=10)
        self.button = Button(self.init_window_name, text="鼠标点击开始", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.mouse_left_click))
        self.button.grid(sticky=W, row=1, column=12)

    def mouse_left_click(self):
        try:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(END, "正在处理请等待.......\n")
            self.result_data_Text.yview_moveto(1)
            starttime = time.time()
            info = ""
            click_count = int(self.click_count.get(1.0, END).replace("\\", "/").replace("\n", ""))
            time.sleep(float(self.start_time.get(1.0, END).replace("\\", "/").replace("\n", "")))
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, "开始点击.......")
            while click_count:
                time.sleep(float(self.run_after_time.get(1.0, END).replace("\\", "/").replace("\n", "")))
                k = PyMouse()
                x, y = k.position()
                k.click(x, y, 1)
                click_count -= 1
            usedtime = time.time() - starttime
            print(usedtime)
            info += "操作总时间：" + str(usedtime) + "s\n"
            info += "点击完成\n"
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, info)
        except RuntimeError as af:
            info = af
        self.result_data_Text.delete(1.0, END)
        self.result_data_Text.insert(1.0, info)

    def download_video_window(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="视频网页地址（多个使用回车键换行）:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="视频保存文件夹路径:", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=4, column=0)
        # 文本框
        self.urls = Text(self.init_window_name, width=75, height=2)  # 输入的文件夹路径为
        self.urls.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10)
        self.dir_path = Entry(self.init_window_name, width=75)
        self.dir_path.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_folder(self.dir_path))
        self.browse_folder_cookies.grid(sticky=W, row=2, column=12)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=4, column=1, rowspan=5, columnspan=10)
        self.button = Button(self.init_window_name, text="开始下载", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.download_video))
        self.button.grid(sticky=W, row=1, column=12)

    def download_video(self):
        self.path1 = os.path.dirname(os.path.abspath(__file__))
        urls = self.urls.get(1.0, END).replace("\\", "/").split("\n")
        dir_name = self.dir_path.get()
        if not os.path.exists(dir_name):  # os模块判断并创建
            os.mkdir(dir_name)
        for video_url in urls:
            try:
                print(video_url)
                time.sleep(random.randint(0, 2))
                if video_url:
                    self.result_data_Text.delete(1.0, END)
                    self.result_data_Text.insert(1.0, "开始下载 " + str(video_url) + "\n")
                    cmd = '%s/you-get.exe -fao %s %s' % (self.path1, dir_name, video_url.split("\n")[0])
                    print(cmd)
                    p = sub.Popen(cmd, stdin=sub.PIPE, stdout=sub.PIPE, stderr=sub.PIPE, shell=True)
                for line in iter(p.stdout.readline, b''):
                    self.result_data_Text.insert(END, line.decode('UTF-8'))
                    self.result_data_Text.yview_moveto(1)
                    if not sub.Popen.poll(p) is None:
                        if line == "":
                            break
                p.stdout.close()
            except Exception as e:
                print(e)
                continue
            self.result_data_Text.insert(1.0, "下载结束 " + str(video_url) + "\n")

    def read_file_to_base64_window(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要处理的图片文件路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=3, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_file(self.file_path))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=3, column=1, rowspan=6, columnspan=10)
        self.button = Button(self.init_window_name, text="转换", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.read_file_to_base64))
        self.button.grid(sticky=W, row=1, column=13)

    def read_file_to_base64(self):
        try:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, "正在处理请等待.......\n")
            starttime = time.time()
            pil_image = open(self.file_path.get(), "rb")
            base64str = base64.b64encode(pil_image.read())
            filename = self.file_path.get()
            index = filename.rfind(".")  # 或者  aa=filename.rindex(".")
            with open(filename[0:index] + ".txt", "w", encoding="utf-8") as ff:
                ff.write(str(base64str).removeprefix("b'").removesuffix("'"))
            usedtime = time.time() - starttime
            print(usedtime)
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, "转换完成!\n")
            self.result_data_Text.insert(END, "文件路径：" + filename[0:index] + ".txt")
        except RuntimeError as rte:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, rte)

    def base64_to_file_window(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="需要处理的base64数据文件路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="转换后文件后缀名（如：txt）", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=3, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                            command=lambda: self.browse_file(self.file_path))
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.last_code = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.last_code.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10, ipady=5)
        self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=3, column=1, rowspan=6, columnspan=10)
        self.button = Button(self.init_window_name, text="转换", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.base64_to_file))
        self.button.grid(sticky=W, row=1, column=13)

    def base64_to_file(self):
        try:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, "正在处理请等待.......\n")
            starttime = time.time()
            base64str = open(self.file_path.get(), "rb")
            pil_image = base64.b64decode(base64str.read())
            filepath = self.file_path.get()
            index = filepath.rfind('.')
            with open(filepath[0:index] + '.' + self.last_code.get(), "wb") as ff:
                ff.write(pil_image)
            usedtime = time.time() - starttime
            print(usedtime)
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, "转换完成!")
            self.result_data_Text.insert(END, "文件路径：" + filepath[0:index] + '.' + self.last_code.get())
        except RuntimeError as rte:
            self.result_data_Text.delete(1.0, END)
            self.result_data_Text.insert(1.0, rte)

    def rename(self):
        for dirpath, dirnames, filenames in os.walk(self.file_path.get()):
            for filename in filenames:
                try:
                    inputfilename = dirpath.replace("\\", "/") + '/' + filename
                    filename = emoji.demojize(filename)
                    print('filename no emoji:' + filename)
                    tables = str.maketrans('', '', "()【】  */" + r"""!"#$%&'()*+，,/:;<=>?@[\]^_`{|}~""" + whitespace)
                    filename = str.strip(filename.translate(tables))
                    # 去除中文
                    pattern = re.compile(u"[\u4e00-\u9fa5]+")
                    filename = re.sub(pattern, "", filename)
                    if inputfilename.__eq__(dirpath.replace("\\", "/") + '/' + filename):
                        continue
                    if os.path.exists(dirpath.replace("\\", "/") + '/' + filename):
                        tt = str(time.time())
                        laststr = filename[filename.rfind("."):]
                        filename = filename[0:filename.index('.')] + tt[tt.index('.'):] + laststr
                    outfilename = dirpath.replace("\\", "/") + '/' + str.strip(filename.translate(tables))
                    print(inputfilename)
                    print(outfilename)
                    os.rename(inputfilename, outfilename)
                    self.result_data_Text.insert(END, inputfilename + " 重命名为：" + outfilename + "\n")
                    self.result_data_Text.yview_moveto(1)
                except FileExistsError as f:
                    print(f)
                    self.result_data_Text.insert(END, f + "\n")
                    self.result_data_Text.yview_moveto(1)
                    continue

    # def deleteBg_windows(self):
    #     self.init_text()
    #     # 标签
    #     self.init_data_label = Label(self.init_window_name, text="需要处理的文件夹路径:", width=40)
    #     self.init_data_label.grid(sticky=E, row=1, column=0)
    #     self.init_data_label3 = Label(self.init_window_name, text="保存文件到文件夹路径:", width=40)
    #     self.init_data_label3.grid(sticky=E, row=2, column=0)
    #     self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
    #     self.result_data_label.grid(sticky=E, row=3, column=0)
    #     # 文本框
    #     self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
    #     self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
    #     self.browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
    #                                         command=lambda: self.browse_folder(self.file_path))
    #     self.browse_folder_cookies.grid(sticky=W, row=1, column=12)
    #     self.file_path2 = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
    #     self.file_path2.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10, ipady=5)
    #     self.browse_folder_cookies2 = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
    #                                          command=lambda: self.browse_folder(self.file_path2))
    #     self.browse_folder_cookies2.grid(sticky=W, row=2, column=12)
    #     self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
    #     self.result_data_Text.grid(sticky=W, row=3, column=1, rowspan=6, columnspan=10)
    #     self.button = Button(self.init_window_name, text="开始处理", font=('楷体', 15), bg="green",
    #                          command=lambda: self.thread_it(self.deleteBg))
    #     self.button.grid(sticky=W, row=1, column=13)
    #
    # def deleteBg(self):
    #     self.result_data_Text.delete(1.0, END)
    #     self.result_data_Text.insert(1.0, "开始处理................\n")
    #     self.rename()
    #     # 加载模型
    #     humanseg = hub.Module(name='deeplabv3p_xception65_humanseg')
    #     # 获取文件列表
    #     for dirpath, dirnames, filenames in os.walk(self.file_path.get()):
    #         for filename in filenames:
    #             try:
    #                 if (filename.__contains__(".jpg") or filename.__contains__(".png")) and not os.path.normcase(
    #                         dirpath) == os.path.normcase(self.file_path2.get()):
    #                     imgpath = os.path.join(dirpath, filename)
    #                     img = Image.open(imgpath)
    #                     # 复制高比宽分辨率大的图片
    #                     if img.size[0] < img.size[1]:
    #                         filess = [imgpath]
    #                         # 抠图
    #                         results = humanseg.segmentation(data={'image': filess},
    #                                                         output_dir=self.file_path2.get())
    #                         for result in results:
    #                             print(result)
    #                             self.result_data_Text.insert(END, result + "\n")
    #                             self.result_data_Text.yview_moveto(1)
    #             except Exception as e:
    #                 print(e)
    #                 continue

    def video_to_img_window(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="视频文件夹路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="图片保存文件夹路径:", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=3, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.v_browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                              command=lambda: self.browse_folder(self.file_path))
        self.v_browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.i_file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.i_file_path.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10, ipady=5)
        self.i_browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                              command=lambda: self.browse_folder(self.i_file_path))
        self.i_browse_folder_cookies.grid(sticky=W, row=2, column=12)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=3, column=1, rowspan=6, columnspan=10)
        self.button = Button(self.init_window_name, text="开始转换", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.video_to_img))
        self.button.grid(sticky=W, row=1, column=13)

    def video_to_img(self):
        self.rename()
        if not os.path.exists(self.i_file_path.get()):
            os.makedirs(self.i_file_path.get())
        for dirpath, dirnames, filenames in os.walk(self.file_path.get()):
            for filename in filenames:
                dirname = os.path.basename(dirpath)
                if not os.path.exists(os.path.join(self.i_file_path.get(), dirname)):
                    os.makedirs(os.path.join(self.i_file_path.get(), dirname))
                self.result_data_Text.insert(END,
                                             "{}开始转换为图片，请耐心等待！\n".format(os.path.join(dirpath, filename)))
                self.result_data_Text.yview_moveto(1)
                cam1 = cv2.VideoCapture(os.path.join(dirpath, filename))
                # 获取视频1的宽度
                ww = int(cam1.get(3))
                # 获取视频1的高度
                hh = int(cam1.get(4))
                print(ww, hh)
                # 获取视频的帧频
                CAMERA_FPS = cam1.get(cv2.CAP_PROP_FPS)  # 25帧/秒
                print('帧频:' + str(CAMERA_FPS))
                # 获取视频的帧数
                CAP_PROP_FRAME_COUNT = cam1.get(cv2.CAP_PROP_FRAME_COUNT)  # 25帧/秒
                print("帧数：" + str(CAP_PROP_FRAME_COUNT))
                num = 0
                while True:
                    # 读取视频
                    (ok1, frame1) = cam1.read()
                    # 视频最后一秒不参与合并到新视频
                    if ok1:
                        filepathnew = os.path.join(self.i_file_path.get(), dirname, filename + str(num) + '.jpg')
                        cv2.imwrite(filepathnew, frame1)
                        num += 1
                    else:
                        print(num)
                        break
                cam1.release()
                self.result_data_Text.insert(END, "{}转换为图片完成\n".format(os.path.join(dirpath, filename)))
                self.result_data_Text.yview_moveto(1)
                cv2.destroyAllWindows()
        self.result_data_Text.insert(END, "转换完成!")
        self.result_data_Text.yview_moveto(1)

    def resize_video_window(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="视频文件夹路径:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="压缩好的文件文件夹路径:", width=40)
        self.init_data_label2.grid(sticky=E, row=2, column=0)
        self.init_data_label3 = Label(self.init_window_name, text="压缩后的文件名前缀:", width=40)
        self.init_data_label3.grid(sticky=E, row=3, column=0)
        self.init_data_label4 = Label(self.init_window_name, text="压缩后的fps:", width=40)
        self.init_data_label4.grid(sticky=E, row=4, column=0)
        self.init_data_label5 = Label(self.init_window_name, text="跳过开头多少秒:", width=40)
        self.init_data_label5.grid(sticky=E, row=5, column=0)
        self.init_data_label6 = Label(self.init_window_name, text="跳过结尾多少秒:", width=40)
        self.init_data_label6.grid(sticky=E, row=6, column=0)
        self.init_data_label7 = Label(self.init_window_name, text="长宽压缩倍数:", width=40)
        self.init_data_label7.grid(sticky=E, row=7, column=0)
        self.init_data_label7 = Label(self.init_window_name, text="跳过帧数间隔:", width=40)
        self.init_data_label7.grid(sticky=E, row=8, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=9, column=0)
        # 文本框
        self.file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.file_path.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.v_browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                              command=lambda: self.browse_folder(self.file_path))
        self.v_browse_folder_cookies.grid(sticky=W, row=1, column=12)
        self.i_file_path = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.i_file_path.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10, ipady=5)
        self.i_browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                              command=lambda: self.browse_folder(self.i_file_path))
        self.i_browse_folder_cookies.grid(sticky=W, row=2, column=12)
        self.i_prefix = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.i_prefix.grid(sticky=W, row=3, column=1, rowspan=1, columnspan=10, ipady=5)
        self.i_fps = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.i_fps.grid(sticky=W, row=4, column=1, rowspan=1, columnspan=10, ipady=5)
        self.i_notresizepre = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.i_notresizepre.grid(sticky=W, row=5, column=1, rowspan=1, columnspan=10, ipady=5)
        self.i_notresizeend = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.i_notresizeend.grid(sticky=W, row=6, column=1, rowspan=1, columnspan=10, ipady=5)
        self.i_w_h_multiple = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.i_w_h_multiple.grid(sticky=W, row=7, column=1, rowspan=1, columnspan=10, ipady=5)
        self.i_jump_fps = Entry(self.init_window_name, width=75)  # 输入的文件夹路径为
        self.i_jump_fps.grid(sticky=W, row=8, column=1, rowspan=1, columnspan=10, ipady=5)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=9, column=1, rowspan=6, columnspan=10)
        self.button = Button(self.init_window_name, text="开始", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.resize_video))
        self.button.grid(sticky=W, row=1, column=13)

    def resize_video(self):
        path = self.file_path.get()  # 原文件文件夹路径
        newPath = self.i_file_path.get()  # 压缩好的文件文件夹路径
        prefix = self.i_prefix.get()  # 压缩好后的文件前缀
        notresizepre = int(self.i_notresizepre.get()) if self.i_notresizepre.get() != '' else 0  # 跳过开头多少秒
        notresizeend = int(self.i_notresizeend.get()) if self.i_notresizeend.get() != '' else 0  # 跳过结尾多少秒
        w_h_multiple = float(self.i_w_h_multiple.get()) if self.i_w_h_multiple.get() != '' else 1  # 长宽压缩倍数
        jump_fps = int(self.i_jump_fps.get()) if self.i_jump_fps.get() != '' else 0  # 跳过帧数
        if not os.path.exists(newPath):
            os.makedirs(newPath)
        for dirpath, dirnames, filenames in os.walk(path):
            count = 1
            for filename in filenames:
                namecount = len(filenames)
                try:
                    cam1 = cv2.VideoCapture(os.path.join(dirpath, filename))
                    # 获取视频1的宽度
                    ww = int(cam1.get(3))
                    # 获取视频1的高度
                    hh = int(cam1.get(4))
                    print(ww, hh)
                    # 获取视频的帧频
                    CAMERA_FPS = cam1.get(cv2.CAP_PROP_FPS)  # 25帧/秒
                    print('帧频:' + str(CAMERA_FPS))
                    # 获取视频的帧数
                    CAP_PROP_FRAME_COUNT = cam1.get(cv2.CAP_PROP_FRAME_COUNT)  # 25帧/秒
                    print("帧数：" + str(CAP_PROP_FRAME_COUNT))
                    # 定义视频编码格式
                    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
                    fps = int(self.i_fps.get()) if '' != self.i_fps.get() else CAMERA_FPS  # fps
                    # 创建视频保存对象
                    videoWriter = cv2.VideoWriter(os.path.join(newPath, prefix + filename[:-4] + '.mp4'),
                                                  fourcc, fps,
                                                  (int(ww * w_h_multiple),
                                                   int(hh * w_h_multiple)))
                    num = 0
                    while True:
                        # 视频最后一秒不参与合并到新视频
                        if CAMERA_FPS * notresizepre <= num <= CAP_PROP_FRAME_COUNT - CAMERA_FPS * notresizeend:
                            # 读取指定num帧数对应视频的帧
                            cam1.set(cv2.CAP_PROP_POS_FRAMES, num)
                            (ok1, frame1) = cam1.read()
                            if ok1:
                                # 重置视频大小，使两视频大小一致
                                if self.i_w_h_multiple.get() != 1:
                                    frame1 = cv2.resize(frame1, (
                                        int(ww * w_h_multiple),
                                        int(hh * w_h_multiple)))
                                videoWriter.write(frame1)
                                if cv2.waitKey(1) & 0xFF == ord('q'):
                                    break
                            else:
                                break
                        elif num > CAP_PROP_FRAME_COUNT - CAMERA_FPS * notresizeend:
                            print(num)
                            break
                        print("文件总数{}，当前视频 {} 为第{}个 {} / {}".format(namecount, filename, count, num,
                                                                               CAP_PROP_FRAME_COUNT))
                        num = num + jump_fps + 1
                        self.result_data_Text.insert(END,
                                                     "文件总数{}，当前视频 {} 为第{}个 {} / {} \n".format(namecount,
                                                                                                         filename,
                                                                                                         count,
                                                                                                         num,
                                                                                                         CAP_PROP_FRAME_COUNT))
                        self.result_data_Text.yview_moveto(1)
                    videoWriter.release()
                    cam1.release()
                    cv2.destroyAllWindows()
                except Exception as e:
                    print(e)
                    self.result_data_Text.insert(END, e)
                    self.result_data_Text.yview_moveto(1)
                    continue
                count += 1
        self.result_data_Text.insert(END, "转换完成!")
        self.result_data_Text.yview_moveto(1)

    def qrcode_window(self):
        self.init_text()
        # 标签
        self.init_data_label = Label(self.init_window_name, text="生成二维码的数据:", width=40)
        self.init_data_label.grid(sticky=E, row=1, column=0)
        self.init_data_label1 = Label(self.init_window_name, text="放入二维码的图片路径（可不填）:", width=40)
        self.init_data_label1.grid(sticky=E, row=2, column=0)
        self.init_data_label2 = Label(self.init_window_name, text="二维码图片保存文件夹路径（可不填）:", width=40)
        self.init_data_label2.grid(sticky=E, row=3, column=0)
        self.result_data_label = Label(self.init_window_name, text="输出结果:", width=40)
        self.result_data_label.grid(sticky=E, row=4, column=0)
        # 文本框
        self.text_value = Entry(self.init_window_name, width=75)  # 生成二维码的数据
        self.text_value.grid(sticky=W, row=1, column=1, rowspan=1, columnspan=10, ipady=5)
        self.file_path = Entry(self.init_window_name, width=75)  # 放入二维码的图片路径
        self.file_path.grid(sticky=W, row=2, column=1, rowspan=1, columnspan=10, ipady=5)
        self.v_browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                              command=lambda: self.browse_file(self.file_path))
        self.v_browse_folder_cookies.grid(sticky=W, row=2, column=12)
        self.i_file_path = Entry(self.init_window_name, width=75)  # 二维码图片保存文件夹路径
        self.i_file_path.grid(sticky=W, row=3, column=1, rowspan=1, columnspan=10, ipady=5)
        self.i_browse_folder_cookies = Button(self.init_window_name, text='浏览', font=('楷体', 15), bg="green",
                                              command=lambda: self.browse_folder(self.i_file_path))
        self.i_browse_folder_cookies.grid(sticky=W, row=3, column=12)
        self.result_data_Text = ScrolledText(self.init_window_name, width=73, height=20)  # 处理结果展示
        self.result_data_Text.grid(sticky=W, row=4, column=1, rowspan=6, columnspan=10)
        self.button = Button(self.init_window_name, text="生成二维码", font=('楷体', 15), bg="green",
                             command=lambda: self.thread_it(self.qrcode))
        self.button.grid(sticky=W, row=1, column=12)

    def qrcode(self):
        # self.rename()
        self.result_data_Text.insert(END, "开始转换生成二维码，请耐心等待！\n")
        self.result_data_Text.yview_moveto(1)
        qr = qrcode.QRCode(version=5, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=8, border=4)
        qr.add_data(self.text_value.get())
        qr.make(fit=True)
        img = qr.make_image()
        img = img.convert("RGB")  # img = img.convert()　　PIL有九种不同模式: 1，L，P，RGB，RGBA，CMYK，YCbCr，I，F。
        if not ''.__eq__(self.file_path.get()):
            icon = Image.open(self.file_path.get())
            img_w, img_h = img.size
            factor = 4
            size_w = int(img_w / factor)
            size_h = int(img_h / factor)
            icon_w, icon_h = icon.size
            if icon_w > size_w:
                icon_w = size_w
            if icon_h > size_h:
                icon_h = size_h
            icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
            w = int((img_w - icon_w) / 2)
            h = int((img_h - icon_h) / 2)
            icon = icon.convert("RGBA")
            img.paste(icon, (w, h), icon)
        if not ''.__eq__(self.i_file_path.get()):
            if not os.path.exists(self.i_file_path.get()):
                os.makedirs(self.i_file_path.get())
            img.save(os.path.join(self.i_file_path.get(), self.text_value.get() + ".jpg"))
            self.result_data_Text.insert(END, "二维码生成成功，保存路径为{}!".format(
                os.path.join(self.i_file_path.get(), self.text_value.get() + ".jpg")))
            self.result_data_Text.yview_moveto(1)
        img.show()
        self.result_data_Text.insert(END, "二维码生成结束！")
        self.result_data_Text.yview_moveto(1)

    # 浏览本地文件夹，选择保存位置
    def browse_folder(self, field_name):
        # 浏览选择本地文件夹
        dir_address = filedialog.askdirectory()
        print(dir_address)
        # 把获得路径，插入保存地址输入框（即插入input_save_address输入框）
        field_name.insert(0, dir_address)

    # 浏览本地文件，选择保存位置
    def browse_file(self, field_name):
        # 浏览选择本地文件夹
        file_address = filedialog.askopenfilename()
        print(file_address)
        # 把获得路径，插入保存地址输入框（即插入input_save_address输入框）
        field_name.insert(0, file_address)

    # 为避免在下载时tkinter界面卡死，创建线程函数
    def thread_it(self, func, *args):
        # 创建
        t = threading.Thread(target=func, args=args)
        # 守护 !!!
        t.setDaemon(True)
        # 启动
        t.start()

    # 获取默认背景图片
    def get_bg_img(self):
        img_base64 = open(os.path.dirname(os.path.abspath(__file__)) + "/" + "my_gui_bg.bs64").read()
        img = base64.b64decode(img_base64.split(",")[1])
        return io.BytesIO(img)


def gui_start():
    init_window = Tk()  # 实例化出一个父窗口
    ZMJ_PORTAL = MY_GUI(init_window)
    # 设置根窗口默认属性
    ZMJ_PORTAL.set_init_window()
    init_window.mainloop()  # 父窗口进入事件循环，可以理解为保持窗口运行，否则界面不展示


'''
pyinstaller无法打包 图片去除背景功能，当准备打包为exe时，需要注释掉对应的菜单、方法
pyinstaller 打包命令： pyinstaller -F my_gui.py --add-data "you-get.exe;." --add-data "my_gui_bg.bs64;." --noconsole
'''
if __name__ == "__main__":
    gui_start()
